home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / sprite / RCS / gcc.c,v < prev    next >
Encoding:
Text File  |  1990-06-28  |  64.9 KB  |  2,756 lines

  1. head     1.8;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.8
  10. date     90.06.28.15.31.26;  author rab;  state Exp;
  11. branches ;
  12. next     1.7;
  13.  
  14. 1.7
  15. date     90.04.09.08.44.35;  author rab;  state Exp;
  16. branches ;
  17. next     1.6;
  18.  
  19. 1.6
  20. date     90.02.06.21.38.18;  author rab;  state Exp;
  21. branches ;
  22. next     1.5;
  23.  
  24. 1.5
  25. date     90.01.12.09.57.51;  author rab;  state Exp;
  26. branches ;
  27. next     1.4;
  28.  
  29. 1.4
  30. date     89.06.15.14.55.55;  author rab;  state Exp;
  31. branches ;
  32. next     1.3;
  33.  
  34. 1.3
  35. date     89.05.09.21.30.22;  author rab;  state Exp;
  36. branches ;
  37. next     1.2;
  38.  
  39. 1.2
  40. date     89.03.12.21.02.06;  author rab;  state Exp;
  41. branches ;
  42. next     1.1;
  43.  
  44. 1.1
  45. date     89.02.24.13.49.55;  author rab;  state Exp;
  46. branches ;
  47. next     ;
  48.  
  49.  
  50. desc
  51. @@
  52.  
  53.  
  54. 1.8
  55. log
  56. @Added support for g++.
  57. @
  58. text
  59. @/* Compiler driver program that can handle many languages.
  60.    Copyright (C) 1987,1989 Free Software Foundation, Inc.
  61.  
  62. This file is part of GNU CC.
  63.  
  64. GNU CC is free software; you can redistribute it and/or modify
  65. it under the terms of the GNU General Public License as published by
  66. the Free Software Foundation; either version 1, or (at your option)
  67. any later version.
  68.  
  69. GNU CC is distributed in the hope that it will be useful,
  70. but WITHOUT ANY WARRANTY; without even the implied warranty of
  71. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  72. GNU General Public License for more details.
  73.  
  74. You should have received a copy of the GNU General Public License
  75. along with GNU CC; see the file COPYING.  If not, write to
  76. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  77.  
  78. This paragraph is here to try to keep Sun CC from dying.
  79. The number of chars here seems crucial!!!!  */
  80.  
  81. void record_temp_file ();
  82.  
  83. /* This program is the user interface to the C compiler and possibly to
  84. other compilers.  It is used because compilation is a complicated procedure
  85. which involves running several programs and passing temporary files between
  86. them, forwarding the users switches to those programs selectively,
  87. and deleting the temporary files at the end.
  88.  
  89. CC recognizes how to compile each input file by suffixes in the file names.
  90. Once it knows which kind of compilation to perform, the procedure for
  91. compilation is specified by a string called a "spec".
  92.  
  93. Specs are strings containing lines, each of which (if not blank)
  94. is made up of a program name, and arguments separated by spaces.
  95. The program name must be exact and start from root, since no path
  96. is searched and it is unreliable to depend on the current working directory.
  97. Redirection of input or output is not supported; the subprograms must
  98. accept filenames saying what files to read and write.
  99.  
  100. In addition, the specs can contain %-sequences to substitute variable text
  101. or for conditional text.  Here is a table of all defined %-sequences.
  102. Note that spaces are not generated automatically around the results of
  103. expanding these sequences; therefore, you can concatenate them together
  104. or with constant text in a single argument.
  105.  
  106.  %%    substitute one % into the program name or argument.
  107.  %i     substitute the name of the input file being processed.
  108.  %b     substitute the basename of the input file being processed.
  109.     This is the substring up to (and not including) the last period.
  110.  %g     substitute the temporary-file-name-base.  This is a string chosen
  111.     once per compilation.  Different temporary file names are made by
  112.     concatenation of constant strings on the end, as in `%g.s'.
  113.     %g also has the same effect of %d.
  114.  %d    marks the argument containing or following the %d as a
  115.     temporary file name, so that that file will be deleted if CC exits
  116.     successfully.  Unlike %g, this contributes no text to the argument.
  117.  %w    marks the argument containing or following the %w as the
  118.     "output file" of this compilation.  This puts the argument
  119.     into the sequence of arguments that %o will substitute later.
  120.  %o    substitutes the names of all the output files, with spaces
  121.     automatically placed around them.  You should write spaces
  122.     around the %o as well or the results are undefined.
  123.     %o is for use in the specs for running the linker.
  124.     Input files whose names have no recognized suffix are not compiled
  125.     at all, but they are included among the output files, so they will
  126.     be linked.
  127.  %p    substitutes the standard macro predefinitions for the
  128.     current target machine.  Use this when running cpp.
  129.  %P    like %p, but puts `__' before and after the name of each macro.
  130.     This is for ANSI C.
  131.  %s     current argument is the name of a library or startup file of some sort.
  132.         Search for that file in a standard list of directories
  133.     and substitute the full pathname found.
  134.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  135.         Use this when inconsistent options are detected.
  136.  %a     process ASM_SPEC as a spec.
  137.         This allows config.h to specify part of the spec for running as.
  138.  %l     process LINK_SPEC as a spec.
  139.  %L     process LIB_SPEC as a spec.
  140.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  141.  %c    process SIGNED_CHAR_SPEC as a spec.
  142.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  143.  %1    process CC1_SPEC as a spec.
  144.  %{S}   substitutes the -S switch, if that switch was given to CC.
  145.     If that switch was not specified, this substitutes nothing.
  146.     Here S is a metasyntactic variable.
  147.  %{S*}  substitutes all the switches specified to CC whose names start
  148.     with -S.  This is used for -o, -D, -I, etc; switches that take
  149.     arguments.  CC considers `-o foo' as being one switch whose
  150.     name starts with `o'.  %{o*} would substitute this text,
  151.     including the space; thus, two arguments would be generated.
  152.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  153.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  154.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  155.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  156.  
  157. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  158. other nested % constructs or spaces, or even newlines.
  159. They are processed as usual, as described above.
  160.  
  161. The character | is used to indicate that a command should be piped to
  162. the following command, but only if -pipe is specified.
  163.  
  164. Note that it is built into CC which switches take arguments and which
  165. do not.  You might think it would be useful to generalize this to
  166. allow each compiler's spec to say which switches take arguments.  But
  167. this cannot be done in a consistent fashion.  CC cannot even decide
  168. which input files have been specified without knowing which switches
  169. take arguments, and it must know which input files to compile in order
  170. to tell which compilers to run.
  171.  
  172. CC also knows implicitly that arguments starting in `-l' are to
  173. be treated as compiler output files, and passed to the linker in their proper
  174. position among the other output files.
  175.  
  176. */
  177.  
  178. #include <stdio.h>
  179. #include <string.h>
  180. #include <sys/types.h>
  181. #include <signal.h>
  182. #include <sys/file.h>
  183. #include <alloca.h>
  184.  
  185. /* #include "config.h" */
  186. #include "obstack.h"
  187. #include "gvarargs.h"
  188.  
  189. #ifdef USG
  190. #define R_OK 4
  191. #define W_OK 2
  192. #define X_OK 1
  193. #define vfork fork
  194. #endif /* USG */
  195.  
  196. #define obstack_chunk_alloc xmalloc
  197. #define obstack_chunk_free free
  198. extern int xmalloc ();
  199. extern void free ();
  200.  
  201. /* If a stage of compilation returns an exit status >= 1,
  202.    compilation of that file ceases.  */
  203.  
  204. #define MIN_FATAL_STATUS 1
  205.  
  206. /* This is the obstack which we use to allocate many strings.  */
  207.  
  208. struct obstack obstack;
  209.  
  210. char *handle_braces ();
  211. char *save_string ();
  212. char *concat ();
  213. int do_spec ();
  214. int do_spec_1 ();
  215. char *find_file ();
  216. static char *find_exec_file ();
  217. void validate_switches ();
  218. void validate_all_switches ();
  219. void fancy_abort ();
  220. static int cplusplus;
  221.  
  222. /*
  223.  * Generate tables containing specs for all known target machines:
  224.  */
  225.  
  226. #include "specs.h"
  227. /* This defines which switch letters take arguments.  */
  228.  
  229. #ifndef SWITCH_TAKES_ARG
  230. #define SWITCH_TAKES_ARG(CHAR)      \
  231.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  232.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  233.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  234.    || (CHAR) == 'L' || (CHAR) == 'i' || (CHAR) == 'A' \
  235.    || (CHAR) == 'B')
  236. #endif
  237.  
  238. /* This defines which multi-letter switches take arguments.  */
  239.  
  240. #ifndef WORD_SWITCH_TAKES_ARG
  241. #define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
  242. #endif
  243.  
  244. /* Accumulate a command (program name and args), and run it.  */
  245.  
  246. /* Vector of pointers to arguments in the current line of specifications.  */
  247.  
  248. char **argbuf;
  249.  
  250. /* Number of elements allocated in argbuf.  */
  251.  
  252. int argbuf_length;
  253.  
  254. /* Number of elements in argbuf currently in use (containing args).  */
  255.  
  256. int argbuf_index;
  257.  
  258. /* Number of commands executed so far.  */
  259.  
  260. int execution_count;
  261.  
  262. /* Flag indicating whether we should print the command and arguments */
  263.  
  264. unsigned char vflag;
  265.  
  266. /* Name with which this program was invoked.  */
  267.  
  268. char *programname;
  269.  
  270. /* User-specified -B prefix to attach to command names,
  271.    or 0 if none specified.  */
  272.  
  273. char *user_exec_prefix = 0;
  274.  
  275. /* Environment-specified prefix to attach to command names,
  276.    or 0 if none specified.  */
  277.  
  278. /* Default prefixes to attach to command names.  If a name contains a
  279.     "%s", then it will be replaced with the type of machine on which
  280.     we're running, to get the correct binaries for this machine. */
  281.  
  282. char *standard_exec_prefix = "/sprite/lib/gcc/%s.md/";
  283. char *standard_exec_prefix_1 = "/sprite/cmds.%s/";
  284.  
  285. char *standard_startfile_prefix = "/sprite/lib/%s.md/";
  286. char *standard_startfile_prefix_1 = "/sprite/lib/gcc/%s.md/";
  287.  
  288. /* Type of machine on which we're running (used to select correct
  289.     binaries for programs like cpp). */
  290.  
  291. char *machine;
  292. int machine_length;        /* No. of characters in machine. */
  293.  
  294. char *env_exec_prefix = 0;
  295.  
  296. /* Suffix to attach to directories searched for commands.  */
  297.  
  298. char *machine_suffix = 0;
  299.  
  300. /* Default prefixes to attach to command names.  */
  301.  
  302. #ifndef STANDARD_EXEC_PREFIX
  303. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  304. #endif /* !defined STANDARD_EXEC_PREFIX */
  305.  
  306. #ifndef STANDARD_STARTFILE_PREFIX
  307. #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  308. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  309.  
  310. /* Clear out the vector of arguments (after a command is executed).  */
  311.  
  312. void
  313. clear_args ()
  314. {
  315.   argbuf_index = 0;
  316. }
  317.  
  318. /* Add one argument to the vector at the end.
  319.    This is done when a space is seen or at the end of the line.
  320.    If DELETE_ALWAYS is nonzero, the arg is a filename
  321.     and the file should be deleted eventually.
  322.    If DELETE_FAILURE is nonzero, the arg is a filename
  323.     and the file should be deleted if this compilation fails.  */
  324.  
  325. void
  326. store_arg (arg, delete_always, delete_failure)
  327.      char *arg;
  328.      int delete_always, delete_failure;
  329. {
  330.   if (argbuf_index + 1 == argbuf_length)
  331.     {
  332.       argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  333.     }
  334.  
  335.   argbuf[argbuf_index++] = arg;
  336.   argbuf[argbuf_index] = 0;
  337.  
  338.   if (delete_always || delete_failure)
  339.     record_temp_file (arg, delete_always, delete_failure);
  340. }
  341.  
  342. /* Record the names of temporary files we tell compilers to write,
  343.    and delete them at the end of the run.  */
  344.  
  345. /* This is the common prefix we use to make temp file names.
  346.    It is chosen once for each run of this program.
  347.    It is substituted into a spec by %g.
  348.    Thus, all temp file names contain this prefix.
  349.    In practice, all temp file names start with this prefix.
  350.  
  351.    This prefix comes from the envvar TMPDIR if it is defined;
  352.    otherwise, from the P_tmpdir macro if that is defined;
  353.    otherwise, in /usr/tmp or /tmp.  */
  354.  
  355. char *temp_filename;
  356.  
  357. /* Length of the prefix.  */
  358.  
  359. int temp_filename_length;
  360.  
  361. /* Define the list of temporary files to delete.  */
  362.  
  363. struct temp_file
  364. {
  365.   char *name;
  366.   struct temp_file *next;
  367. };
  368.  
  369. /* Queue of files to delete on success or failure of compilation.  */
  370. struct temp_file *always_delete_queue;
  371. /* Queue of files to delete on failure of compilation.  */
  372. struct temp_file *failure_delete_queue;
  373.  
  374. /* Record FILENAME as a file to be deleted automatically.
  375.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  376.    otherwise delete it in any case.
  377.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  378.    otherwise delete it in any case.  */
  379.  
  380. void
  381. record_temp_file (filename, always_delete, fail_delete)
  382.      char *filename;
  383.      int always_delete;
  384.      int fail_delete;
  385. {
  386.   register char *name;
  387.   name = (char *) xmalloc (strlen (filename) + 1);
  388.   strcpy (name, filename);
  389.  
  390.   if (always_delete)
  391.     {
  392.       register struct temp_file *temp;
  393.       for (temp = always_delete_queue; temp; temp = temp->next)
  394.     if (! strcmp (name, temp->name))
  395.       goto already1;
  396.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  397.       temp->next = always_delete_queue;
  398.       temp->name = name;
  399.       always_delete_queue = temp;
  400.     already1:;
  401.     }
  402.  
  403.   if (fail_delete)
  404.     {
  405.       register struct temp_file *temp;
  406.       for (temp = failure_delete_queue; temp; temp = temp->next)
  407.     if (! strcmp (name, temp->name))
  408.       goto already2;
  409.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  410.       temp->next = failure_delete_queue;
  411.       temp->name = name;
  412.       failure_delete_queue = temp;
  413.     already2:;
  414.     }
  415. }
  416.  
  417. /* Delete all the temporary files whose names we previously recorded.  */
  418.  
  419. void
  420. delete_temp_files ()
  421. {
  422.   register struct temp_file *temp;
  423.  
  424.   for (temp = always_delete_queue; temp; temp = temp->next)
  425.     {
  426. #ifdef DEBUG
  427.       int i;
  428.       printf ("Delete %s? (y or n) ", temp->name);
  429.       fflush (stdout);
  430.       i = getchar ();
  431.       if (i != '\n')
  432.     while (getchar () != '\n') ;
  433.       if (i == 'y' || i == 'Y')
  434. #endif /* DEBUG */
  435.     {
  436.       if (unlink (temp->name) < 0)
  437.         if (vflag)
  438.           perror_with_name (temp->name);
  439.     }
  440.     }
  441.  
  442.   always_delete_queue = 0;
  443. }
  444.  
  445. /* Delete all the files to be deleted on error.  */
  446.  
  447. void
  448. delete_failure_queue ()
  449. {
  450.   register struct temp_file *temp;
  451.  
  452.   for (temp = failure_delete_queue; temp; temp = temp->next)
  453.     {
  454. #ifdef DEBUG
  455.       int i;
  456.       printf ("Delete %s? (y or n) ", temp->name);
  457.       fflush (stdout);
  458.       i = getchar ();
  459.       if (i != '\n')
  460.     while (getchar () != '\n') ;
  461.       if (i == 'y' || i == 'Y')
  462. #endif /* DEBUG */
  463.     {
  464.       if (unlink (temp->name) < 0)
  465.         if (vflag)
  466.           perror_with_name (temp->name);
  467.     }
  468.     }
  469. }
  470.  
  471. void
  472. clear_failure_queue ()
  473. {
  474.   failure_delete_queue = 0;
  475. }
  476.  
  477. /* Compute a string to use as the base of all temporary file names.
  478.    It is substituted for %g.  */
  479.  
  480. void
  481. choose_temp_base ()
  482. {
  483.   extern char *getenv ();
  484.   char *base = getenv ("TMPDIR");
  485.   int len;
  486.  
  487.   if (base == (char *)0)
  488.     {
  489. #ifdef P_tmpdir
  490.       if (access (P_tmpdir, R_OK | W_OK) == 0)
  491.     base = P_tmpdir;
  492. #endif
  493.       if (base == (char *)0)
  494.     {
  495.       if (access ("/usr/tmp", R_OK | W_OK) == 0)
  496.         base = "/usr/tmp/";
  497.       else
  498.         base = "/tmp/";
  499.     }
  500.     }
  501.  
  502.   len = strlen (base);
  503.   temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
  504.   strcpy (temp_filename, base);
  505.   if (len > 0 && temp_filename[len-1] != '/')
  506.     temp_filename[len++] = '/';
  507.   strcpy (temp_filename + len, "ccXXXXXX");
  508.  
  509.   mktemp (temp_filename);
  510.   temp_filename_length = strlen (temp_filename);
  511. }
  512.  
  513. /* Search for an execute file through our search path.
  514.    Return 0 if not found, otherwise return its name, allocated with malloc.  */
  515.  
  516. static char *
  517. find_exec_file (prog)
  518.      char *prog;
  519. {
  520.   int win = 0;
  521.   char *temp;
  522.   int size;
  523.  
  524.   size = strlen (standard_exec_prefix);
  525.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  526.     size = strlen (user_exec_prefix);
  527.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  528.     size = strlen (env_exec_prefix);
  529.   if (strlen (standard_exec_prefix_1) > size)
  530.     size = strlen (standard_exec_prefix_1);
  531.   size += strlen (prog) + machine_length + 1;
  532.   if (machine_suffix)
  533.     size += strlen (machine_suffix) + 1;
  534.   temp = (char *) xmalloc (size);
  535.  
  536.   /* Determine the filename to execute.  */
  537.  
  538.   if (user_exec_prefix)
  539.     {
  540.       sprintf (temp, user_exec_prefix, machine);        
  541.       strcat (temp, prog);
  542.       win = (access (temp, X_OK) == 0);
  543.     }
  544.  
  545.   if (!win && env_exec_prefix)
  546.     {
  547.       sprintf(temp, env_exec_prefix, machine);
  548.       strcat (temp, prog);
  549.       win = (access (temp, X_OK) == 0);
  550.     }
  551.  
  552.   if (!win)
  553.     {
  554.       sprintf (temp, standard_exec_prefix, machine);
  555.       strcat (temp, prog);
  556.       win = (access (temp, X_OK) == 0);
  557.     }
  558.  
  559.   if (!win)
  560.     {
  561.       sprintf (temp, standard_exec_prefix_1, machine);
  562.       strcat (temp, prog);
  563.       win = (access (temp, X_OK) == 0);
  564.     }
  565.  
  566.   if (win)
  567.     return temp;
  568.   else
  569.     return 0;
  570. }
  571.  
  572. /* stdin file number.  */
  573. #define STDIN_FILE_NO 0
  574.  
  575. /* stdout file number.  */
  576. #define STDOUT_FILE_NO 1
  577.  
  578. /* value of `pipe': port index for reading.  */
  579. #define READ_PORT 0
  580.  
  581. /* value of `pipe': port index for writing.  */
  582. #define WRITE_PORT 1
  583.  
  584. /* Pipe waiting from last process, to be used as input for the next one.
  585.    Value is STDIN_FILE_NO if no pipe is waiting
  586.    (i.e. the next command is the first of a group).  */
  587.  
  588. int last_pipe_input;
  589.  
  590. /* Fork one piped subcommand.  FUNC is the system call to use
  591.    (either execv or execvp).  ARGV is the arg vector to use.
  592.    NOT_LAST is nonzero if this is not the last subcommand
  593.    (i.e. its output should be piped to the next one.)  */
  594.  
  595. static int
  596. pexecute (func, program, argv, not_last)
  597.      char *program;
  598.      int (*func)();
  599.      char *argv[];
  600.      int not_last;
  601. {
  602.   int pid;
  603.   int pdes[2];
  604.   int input_desc = last_pipe_input;
  605.   int output_desc = STDOUT_FILE_NO;
  606.  
  607.   /* If this isn't the last process, make a pipe for its output,
  608.      and record it as waiting to be the input to the next process.  */
  609.  
  610.   if (not_last)
  611.     {
  612.       if (pipe (pdes) < 0)
  613.     pfatal_with_name ("pipe");
  614.       output_desc = pdes[WRITE_PORT];
  615.       last_pipe_input = pdes[READ_PORT];
  616.     }
  617.   else
  618.     last_pipe_input = STDIN_FILE_NO;
  619.  
  620.   pid = vfork ();
  621.  
  622.   switch (pid)
  623.     {
  624.     case -1:
  625.       pfatal_with_name ("vfork");
  626.       break;
  627.  
  628.     case 0: /* child */
  629.       /* Move the input and output pipes into place, if nec.  */
  630.       if (input_desc != STDIN_FILE_NO)
  631.     {
  632.       close (STDIN_FILE_NO);
  633.       dup (input_desc);
  634.       close (input_desc);
  635.     }
  636.       if (output_desc != STDOUT_FILE_NO)
  637.     {
  638.       close (STDOUT_FILE_NO);
  639.       dup (output_desc);
  640.       close (output_desc);
  641.     }
  642.  
  643.       /* Close the parent's descs that aren't wanted here.  */
  644.       if (last_pipe_input != STDIN_FILE_NO)
  645.     close (last_pipe_input);
  646.  
  647.       /* Exec the program.  */
  648.       (*func) (program, argv);
  649.       perror_exec (program);
  650.       exit (-1);
  651.       /* NOTREACHED */
  652.  
  653.     default:
  654.       /* In the parent, after forking.
  655.      Close the descriptors that we made for this child.  */
  656.       if (input_desc != STDIN_FILE_NO)
  657.     close (input_desc);
  658.       if (output_desc != STDOUT_FILE_NO)
  659.     close (output_desc);
  660.  
  661.       /* Return child's process number.  */
  662.       return pid;
  663.     }
  664. }
  665.  
  666. /* Execute the command specified by the arguments on the current line of spec.
  667.    When using pipes, this includes several piped-together commands
  668.    with `|' between them.
  669.  
  670.    Return 0 if successful, -1 if failed.  */
  671.  
  672. int
  673. execute ()
  674. {
  675.   int i, j;
  676.   int n_commands;        /* # of command.  */
  677.   char *string;
  678.   struct command
  679.     {
  680.       char *prog;        /* program name.  */
  681.       char **argv;        /* vector of args.  */
  682.       int pid;            /* pid of process for this command.  */
  683.     };
  684.  
  685.   struct command *commands;    /* each command buffer with above info.  */
  686.  
  687.   /* Count # of piped commands.  */
  688.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  689.     if (strcmp (argbuf[i], "|") == 0)
  690.       n_commands++;
  691.  
  692.   /* Get storage for each command.  */
  693.   commands
  694.     = (struct command *) alloca (n_commands * sizeof (struct command));
  695.  
  696.   /* Split argbuf into its separate piped processes,
  697.      and record info about each one.
  698.      Also search for the programs that are to be run.  */
  699.  
  700.   commands[0].prog = argbuf[0]; /* first command.  */
  701.   commands[0].argv = &argbuf[0];
  702.   string = find_exec_file (commands[0].prog);
  703.   if (string)
  704.     commands[0].argv[0] = string;
  705.  
  706.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  707.     if (strcmp (argbuf[i], "|") == 0)
  708.       {                /* each command.  */
  709.     argbuf[i] = 0;    /* termination of command args.  */
  710.     commands[n_commands].prog = argbuf[i + 1];
  711.     commands[n_commands].argv = &argbuf[i + 1];
  712.     string = find_exec_file (commands[n_commands].prog);
  713.     if (string)
  714.       commands[n_commands].argv[0] = string;
  715.     n_commands++;
  716.       }
  717.  
  718.   argbuf[argbuf_index] = 0;
  719.  
  720.   /* If -v, print what we are about to do, and maybe query.  */
  721.  
  722.   if (vflag)
  723.     {
  724.       /* Print each piped command as a separate line.  */
  725.       for (i = 0; i < n_commands ; i++)
  726.     {
  727.       char **j;
  728.  
  729.       for (j = commands[i].argv; *j; j++)
  730.         fprintf (stderr, " %s", *j);
  731.  
  732.       /* Print a pipe symbol after all but the last command.  */
  733.       if (i + 1 != n_commands)
  734.         fprintf (stderr, " |");
  735.       fprintf (stderr, "\n");
  736.     }
  737.       fflush (stderr);
  738. #ifdef DEBUG
  739.       fprintf (stderr, "\nGo ahead? (y or n) ");
  740.       fflush (stderr);
  741.       j = getchar ();
  742.       if (j != '\n')
  743.     while (getchar () != '\n') ;
  744.       if (j != 'y' && j != 'Y')
  745.     return 0;
  746. #endif /* DEBUG */
  747.     }
  748.  
  749.   /* Run each piped subprocess.  */
  750.  
  751.   last_pipe_input = STDIN_FILE_NO;
  752.   for (i = 0; i < n_commands; i++)
  753.     {
  754.       extern int execv(), execvp();
  755.       char *string = commands[i].argv[0];
  756.  
  757.       commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
  758.                   string, commands[i].argv,
  759.                   i + 1 < n_commands);
  760.  
  761.       if (string != commands[i].prog)
  762.     free (string);
  763.     }
  764.  
  765.   execution_count++;
  766.  
  767.   /* Wait for all the subprocesses to finish.
  768.      We don't care what order they finish in;
  769.      we know that N_COMMANDS waits will get them all.  */
  770.  
  771.   {
  772.     int ret_code = 0;
  773.  
  774.     for (i = 0; i < n_commands; i++)
  775.       {
  776.     int status;
  777.     int pid;
  778.     char *prog;
  779.  
  780.     pid = wait (&status);
  781.     if (pid < 0)
  782.       abort ();
  783.  
  784.     if (status != 0)
  785.       {
  786.         int j;
  787.         for (j = 0; j < n_commands; j++)
  788.           if (commands[j].pid == pid)
  789.         prog = commands[j].prog;
  790.  
  791.         if ((status & 0x7F) != 0)
  792.           fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
  793.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  794.           ret_code = -1;
  795.       }
  796.       }
  797.     return ret_code;
  798.   }
  799. }
  800.  
  801. void
  802. set_target_machine(name)
  803.   char *name;
  804. {
  805.   register target_machine *t;
  806.   int i;
  807.  
  808.   for (i = 0; ; i++)
  809.   {
  810.     t = &target_machines[i];
  811.     if (t->name == 0)
  812.       {
  813.       return;
  814.       }
  815.     if (strcmp(t->name, name) == 0)
  816.       {
  817.         target_name = t->info->name;
  818.     target = t->info;
  819.     return;
  820.       }
  821.   }
  822. }
  823.  
  824. /* Find all the switches given to us
  825.    and make a vector describing them.
  826.    The elements of the vector a strings, one per switch given.
  827.    If a switch uses the following argument, then the `part1' field
  828.    is the switch itself and the `part2' field is the following argument.
  829.    The `valid' field is nonzero if any spec has looked at this switch;
  830.    if it remains zero at the end of the run, it must be meaningless.  */
  831.  
  832. struct switchstr
  833. {
  834.   char *part1;
  835.   char *part2;
  836.   int valid;
  837. };
  838.  
  839. struct switchstr *switches;
  840.  
  841. int n_switches;
  842.  
  843. /* Also a vector of input files specified.  */
  844.  
  845. char **infiles;
  846.  
  847. int n_infiles;
  848.  
  849. /* And a vector of corresponding output files is made up later.  */
  850.  
  851. char **outfiles;
  852.  
  853. /* Create the vector `switches' and its contents.
  854.    Store its length in `n_switches'.  */
  855.  
  856. void
  857. process_command (argc, argv)
  858.      int argc;
  859.      char **argv;
  860. {
  861.   extern char *getenv ();
  862.   register int i;
  863.   n_switches = 0;
  864.   n_infiles = 0;
  865.  
  866.   env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  867.  
  868.   /* Scan argv twice.  Here, the first time, just count how many switches
  869.      there will be in their vector, and how many input files in theirs.
  870.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  871.  
  872.   for (i = 1; i < argc; i++)
  873.     {
  874.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  875.     {
  876.       register char *p = &argv[i][1];
  877.       register int c = *p;
  878.  
  879.       switch (c)
  880.         {
  881.         case 'b':
  882.           machine_suffix = p + 1;
  883.           break;
  884.  
  885.         case 'B':
  886.           user_exec_prefix = p + 1;
  887.           break;
  888.  
  889.         case 'v':    /* Print our subcommands and print versions.  */
  890.           vflag++;
  891.           n_switches++;
  892.           break;
  893.  
  894.         case 'm':
  895.           /*
  896.            * See if this switch identifies a known target machine.
  897.            * If so, use it to set the list of specs for the target.
  898.            */
  899.           set_target_machine(p + 1);
  900.           n_switches++;
  901.           break;
  902.  
  903.         default:
  904.           n_switches++;
  905.  
  906.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  907.         i++;
  908.           else if (WORD_SWITCH_TAKES_ARG (p))
  909.         i++;
  910.         }
  911.     }
  912.       else
  913.     n_infiles++;
  914.     }
  915.  
  916.   /* Then create the space for the vectors and scan again.  */
  917.  
  918.   switches = ((struct switchstr *)
  919.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  920.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  921.   n_switches = 0;
  922.   n_infiles = 0;
  923.  
  924.   /* This, time, copy the text of each switch and store a pointer
  925.      to the copy in the vector of switches.
  926.      Store all the infiles in their vector.  */
  927.  
  928.   for (i = 1; i < argc; i++)
  929.     {
  930.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  931.     {
  932.       register char *p = &argv[i][1];
  933.       register int c = *p;
  934.  
  935.       if (c == 'b')
  936.         continue;
  937.       switches[n_switches].part1 = p;
  938.       if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
  939.           || WORD_SWITCH_TAKES_ARG (p))
  940.         switches[n_switches].part2 = argv[++i];
  941.       else
  942.         switches[n_switches].part2 = 0;
  943.       switches[n_switches].valid = 0;
  944.       if (c != 'B')
  945.           n_switches++;
  946.     }
  947.       else
  948.     infiles[n_infiles++] = argv[i];
  949.     }
  950.  
  951.   switches[n_switches].part1 = 0;
  952.   infiles[n_infiles] = 0;
  953. }
  954.  
  955. /* Process a spec string, accumulating and running commands.  */
  956.  
  957. /* These variables describe the input file name.
  958.    input_file_number is the index on outfiles of this file,
  959.    so that the output file name can be stored for later use by %o.
  960.    input_basename is the start of the part of the input file
  961.    sans all directory names, and basename_length is the number
  962.    of characters starting there excluding the suffix .c or whatever.  */
  963.  
  964. char *input_filename;
  965. int input_file_number;
  966. int input_filename_length;
  967. int basename_length;
  968. char *input_basename;
  969.  
  970. /* These are variables used within do_spec and do_spec_1.  */
  971.  
  972. /* Nonzero if an arg has been started and not yet terminated
  973.    (with space, tab or newline).  */
  974. int arg_going;
  975.  
  976. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  977.    is a temporary file name.  */
  978. int delete_this_arg;
  979.  
  980. /* Nonzero means %w has been seen; the next arg to be terminated
  981.    is the output file name of this compilation.  */
  982. int this_is_output_file;
  983.  
  984. /* Nonzero means %s has been seen; the next arg to be terminated
  985.    is the name of a library file and we should try the standard
  986.    search dirs for it.  */
  987. int this_is_library_file;
  988.  
  989. /* Process the spec SPEC and run the commands specified therein.
  990.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  991.  
  992. int
  993. do_spec (spec)
  994.      char *spec;
  995. {
  996.   int value;
  997.  
  998.   clear_args ();
  999.   arg_going = 0;
  1000.   delete_this_arg = 0;
  1001.   this_is_output_file = 0;
  1002.   this_is_library_file = 0;
  1003.  
  1004.   value = do_spec_1 (spec, 0);
  1005.  
  1006.   /* Force out any unfinished command.
  1007.      If -pipe, this forces out the last command if it ended in `|'.  */
  1008.   if (value == 0)
  1009.     {
  1010.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1011.     argbuf_index--;
  1012.  
  1013.       if (argbuf_index > 0)
  1014.     value = execute ();
  1015.     }
  1016.  
  1017.   return value;
  1018. }
  1019.  
  1020. /* Process the sub-spec SPEC as a portion of a larger spec.
  1021.    This is like processing a whole spec except that we do
  1022.    not initialize at the beginning and we do not supply a
  1023.    newline by default at the end.
  1024.    INSWITCH nonzero means don't process %-sequences in SPEC;
  1025.    in this case, % is treated as an ordinary character.
  1026.    This is used while substituting switches.
  1027.    INSWITCH nonzero also causes SPC not to terminate an argument.
  1028.  
  1029.    Value is zero unless a line was finished
  1030.    and the command on that line reported an error.  */
  1031.  
  1032. int
  1033. do_spec_1 (spec, inswitch)
  1034.      char *spec;
  1035.      int inswitch;
  1036. {
  1037.   register char *p = spec;
  1038.   register int c;
  1039.   char *string;
  1040.  
  1041.   while (c = *p++)
  1042.     /* If substituting a switch, treat all chars like letters.
  1043.        Otherwise, NL, SPC, TAB and % are special.  */
  1044.     switch (inswitch ? 'a' : c)
  1045.       {
  1046.       case '\n':
  1047.     /* End of line: finish any pending argument,
  1048.        then run the pending command if one has been started.  */
  1049.     if (arg_going)
  1050.       {
  1051.         obstack_1grow (&obstack, 0);
  1052.         string = obstack_finish (&obstack);
  1053.         if (this_is_library_file)
  1054.           string = find_file (string);
  1055.         store_arg (string, delete_this_arg, this_is_output_file);
  1056.         if (this_is_output_file)
  1057.           outfiles[input_file_number] = string;
  1058.       }
  1059.     arg_going = 0;
  1060.  
  1061.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1062.       {
  1063.         int i;
  1064.         for (i = 0; i < n_switches; i++)
  1065.           if (!strcmp (switches[i].part1, "pipe"))
  1066.         break;
  1067.  
  1068.         /* A `|' before the newline means use a pipe here,
  1069.            but only if -pipe was specified.
  1070.            Otherwise, execute now and don't pass the `|' as an arg.  */
  1071.         if (i < n_switches)
  1072.           {
  1073.         switches[i].valid = 1;
  1074.         break;
  1075.           }
  1076.         else
  1077.           argbuf_index--;
  1078.       }
  1079.  
  1080.     if (argbuf_index > 0)
  1081.       {
  1082.         int value = execute ();
  1083.         if (value)
  1084.           return value;
  1085.       }
  1086.     /* Reinitialize for a new command, and for a new argument.  */
  1087.     clear_args ();
  1088.     arg_going = 0;
  1089.     delete_this_arg = 0;
  1090.     this_is_output_file = 0;
  1091.     this_is_library_file = 0;
  1092.     break;
  1093.  
  1094.       case '|':
  1095.     /* End any pending argument.  */
  1096.     if (arg_going)
  1097.       {
  1098.         obstack_1grow (&obstack, 0);
  1099.         string = obstack_finish (&obstack);
  1100.         if (this_is_library_file)
  1101.           string = find_file (string);
  1102.         store_arg (string, delete_this_arg, this_is_output_file);
  1103.         if (this_is_output_file)
  1104.           outfiles[input_file_number] = string;
  1105.       }
  1106.  
  1107.     /* Use pipe */
  1108.     obstack_1grow (&obstack, c);
  1109.     arg_going = 1;
  1110.     break;
  1111.  
  1112.       case '\t':
  1113.       case ' ':
  1114.     /* Space or tab ends an argument if one is pending.  */
  1115.     if (arg_going)
  1116.       {
  1117.         obstack_1grow (&obstack, 0);
  1118.         string = obstack_finish (&obstack);
  1119.         if (this_is_library_file)
  1120.           string = find_file (string);
  1121.         store_arg (string, delete_this_arg, this_is_output_file);
  1122.         if (this_is_output_file)
  1123.           outfiles[input_file_number] = string;
  1124.       }
  1125.     /* Reinitialize for a new argument.  */
  1126.     arg_going = 0;
  1127.     delete_this_arg = 0;
  1128.     this_is_output_file = 0;
  1129.     this_is_library_file = 0;
  1130.     break;
  1131.  
  1132.       case '%':
  1133.     switch (c = *p++)
  1134.       {
  1135.       case 0:
  1136.         fatal ("Invalid specification!  Bug in cc.");
  1137.  
  1138.       case 'b':
  1139.         obstack_grow (&obstack, input_basename, basename_length);
  1140.         arg_going = 1;
  1141.         break;
  1142.  
  1143.       case 'd':
  1144.         delete_this_arg = 2;
  1145.         break;
  1146.  
  1147.       case 'e':
  1148.         /* {...:%efoo} means report an error with `foo' as error message
  1149.            and don't execute any more commands for this file.  */
  1150.         {
  1151.           char *q = p;
  1152.           char *buf;
  1153.           while (*p != 0 && *p != '\n') p++;
  1154.           buf = (char *) alloca (p - q + 1);
  1155.           strncpy (buf, q, p - q);
  1156.           error ("%s", buf);
  1157.           return -1;
  1158.         }
  1159.         break;
  1160.  
  1161.       case 'g':
  1162.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  1163.         delete_this_arg = 1;
  1164.         arg_going = 1;
  1165.         break;
  1166.  
  1167.       case 'i':
  1168.         obstack_grow (&obstack, input_filename, input_filename_length);
  1169.         arg_going = 1;
  1170.         break;
  1171.  
  1172.       case 'o':
  1173.         {
  1174.           register int f;
  1175.           for (f = 0; f < n_infiles; f++)
  1176.         store_arg (outfiles[f], 0, 0);
  1177.         }
  1178.         break;
  1179.  
  1180.       case 's':
  1181.         this_is_library_file = 1;
  1182.         break;
  1183.  
  1184.       case 'w':
  1185.         this_is_output_file = 1;
  1186.         break;
  1187.  
  1188.       case '{':
  1189.         p = handle_braces (p);
  1190.         if (p == 0)
  1191.           return -1;
  1192.         break;
  1193.  
  1194.       case '%':
  1195.         obstack_1grow (&obstack, '%');
  1196.         break;
  1197.  
  1198. /*** The rest just process a certain constant string as a spec.  */
  1199.  
  1200.           case '+':
  1201.         do_spec_1 (target->cplus1_spec, 0);
  1202.         break;
  1203.  
  1204.       case '1':
  1205.         do_spec_1 (target->cc1_spec, 0);
  1206.         break;
  1207.  
  1208.       case 'a':
  1209.         do_spec_1 (target->asm_spec, 0);
  1210.         break;
  1211.  
  1212.       case 'c':
  1213.         do_spec_1 (target->signed_char_spec, 0);
  1214.         break;
  1215.  
  1216.       case 'C':
  1217.         do_spec_1 (target->cpp_spec, 0);
  1218.         break;
  1219.  
  1220.       case 'l':
  1221.         do_spec_1 (target->link_spec, 0);
  1222.         break;
  1223.  
  1224.       case 'L':
  1225.         do_spec_1 (target->lib_spec, 0);
  1226.         break;
  1227.  
  1228.       case 'm':
  1229.         obstack_grow (&obstack, target_name, strlen (target_name));
  1230.         arg_going = 1;
  1231.         break;
  1232.  
  1233.       case 'p':
  1234.         do_spec_1 (target->cpp_predefines, 0);
  1235.         break;
  1236.  
  1237.       case 'P':
  1238.         {
  1239.           char *x = (char *) alloca(strlen(target->cpp_predefines) * 2 + 1);
  1240.           char *buf = x;
  1241.           char *y = target->cpp_predefines;
  1242.  
  1243.           /* Copy all of target->cpp_predefines into BUF,
  1244.          but put __ after every -D and at the end of each arg,  */
  1245.           while (1)
  1246.         {
  1247.           if (! strncmp (y, "-D", 2))
  1248.             {
  1249.               *x++ = '-';
  1250.               *x++ = 'D';
  1251.               *x++ = '_';
  1252.               *x++ = '_';
  1253.               y += 2;
  1254.             }
  1255.           else if (*y == ' ' || *y == 0)
  1256.             {
  1257.               *x++ = '_';
  1258.               *x++ = '_';
  1259.               if (*y == 0)
  1260.             break;
  1261.               else
  1262.             *x++ = *y++;
  1263.             }
  1264.           else
  1265.             *x++ = *y++;
  1266.         }
  1267.           *x = 0;
  1268.  
  1269.           do_spec_1 (buf, 0);
  1270.         }
  1271.         break;
  1272.  
  1273.       case 'S':
  1274.         do_spec_1 (target->startfile_spec, 0);
  1275.         break;
  1276.  
  1277.       default:
  1278.         fatal ("Invalid spec sequence \"%%%c\"!  Bug in cc.", c);
  1279.       }
  1280.     break;
  1281.  
  1282.       default:
  1283.     /* Ordinary character: put it into the current argument.  */
  1284.     obstack_1grow (&obstack, c);
  1285.     arg_going = 1;
  1286.       }
  1287.  
  1288.   return 0;        /* End of string */
  1289. }
  1290.  
  1291. /* Return 0 if we call do_spec_1 and that returns -1.  */
  1292.  
  1293. char *
  1294. handle_braces (p)
  1295.      register char *p;
  1296. {
  1297.   register char *q;
  1298.   char *filter;
  1299.   int pipe = 0;
  1300.   int negate = 0;
  1301.  
  1302.   if (*p == '|')
  1303.     /* A `|' after the open-brace means,
  1304.        if the test fails, output a single minus sign rather than nothing.
  1305.        This is used in %{|!pipe:...}.  */
  1306.     pipe = 1, ++p;
  1307.  
  1308.   if (*p == '!')
  1309.     /* A `!' after the open-brace negates the condition:
  1310.        succeed if the specified switch is not present.  */
  1311.     negate = 1, ++p;
  1312.  
  1313.   filter = p;
  1314.   while (*p != ':' && *p != '}') p++;
  1315.   if (*p != '}')
  1316.     {
  1317.       register int count = 1;
  1318.       q = p + 1;
  1319.       while (count > 0)
  1320.     {
  1321.       if (*q == '{')
  1322.         count++;
  1323.       else if (*q == '}')
  1324.         count--;
  1325.       else if (*q == 0)
  1326.         abort ();
  1327.       q++;
  1328.     }
  1329.     }
  1330.   else
  1331.     q = p + 1;
  1332.  
  1333.   if (p[-1] == '*' && p[0] == '}')
  1334.     {
  1335.       /* Substitute all matching switches as separate args.  */
  1336.       register int i;
  1337.       --p;
  1338.       for (i = 0; i < n_switches; i++)
  1339.     if (!strncmp (switches[i].part1, filter, p - filter))
  1340.       give_switch (i);
  1341.     }
  1342.   else
  1343.     {
  1344.       /* Test for presence of the specified switch.  */
  1345.       register int i;
  1346.       int present = 0;
  1347.  
  1348.       /* If name specified ends in *, as in {x*:...},
  1349.      check for presence of any switch name starting with x.  */
  1350.       if (p[-1] == '*')
  1351.     {
  1352.       for (i = 0; i < n_switches; i++)
  1353.         {
  1354.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  1355.         {
  1356.           switches[i].valid = 1;
  1357.           present = 1;
  1358.         }
  1359.         }
  1360.     }
  1361.       /* Otherwise, check for presence of exact name specified.  */
  1362.       else
  1363.     {
  1364.       for (i = 0; i < n_switches; i++)
  1365.         {
  1366.           if (!strncmp (switches[i].part1, filter, p - filter)
  1367.           && switches[i].part1[p - filter] == 0)
  1368.         {
  1369.           switches[i].valid = 1;
  1370.           present = 1;
  1371.           break;
  1372.         }
  1373.         }
  1374.     }
  1375.  
  1376.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  1377.      then substitute either the switch or the specified
  1378.      conditional text.  */
  1379.       if (present != negate)
  1380.     {
  1381.       if (*p == '}')
  1382.         {
  1383.           give_switch (i);
  1384.         }
  1385.       else
  1386.         {
  1387.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  1388.         return 0;
  1389.         }
  1390.     }
  1391.       else if (pipe)
  1392.     {
  1393.       /* Here if a %{|...} conditional fails: output a minus sign,
  1394.          which means "standard output" or "standard input".  */
  1395.       do_spec_1 ("-", 0);
  1396.     }
  1397.     }
  1398.  
  1399.   return q;
  1400. }
  1401.  
  1402. /* Pass a switch to the current accumulating command
  1403.    in the same form that we received it.
  1404.    SWITCHNUM identifies the switch; it is an index into
  1405.    the vector of switches gcc received, which is `switches'.
  1406.    This cannot fail since it never finishes a command line.  */
  1407.  
  1408. give_switch (switchnum)
  1409.      int switchnum;
  1410. {
  1411.   do_spec_1 ("-", 0);
  1412.   do_spec_1 (switches[switchnum].part1, 1);
  1413.   do_spec_1 (" ", 0);
  1414.   if (switches[switchnum].part2 != 0)
  1415.     {
  1416.       do_spec_1 (switches[switchnum].part2, 1);
  1417.       do_spec_1 (" ", 0);
  1418.     }
  1419.   switches[switchnum].valid = 1;
  1420. }
  1421.  
  1422. /* Search for a file named NAME trying various prefixes including the
  1423.    user's -B prefix and some standard ones.
  1424.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  1425.  
  1426. char *
  1427. find_file (name)
  1428.      char *name;
  1429. {
  1430.   int size;
  1431.   char *temp;
  1432.   int win = 0;
  1433.  
  1434.   /* Compute maximum size of NAME plus any prefix we will try.  */
  1435.  
  1436.   size = strlen (standard_exec_prefix);
  1437.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  1438.     size = strlen (user_exec_prefix);
  1439.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  1440.     size = strlen (env_exec_prefix);
  1441.   if (strlen (standard_exec_prefix) > size)
  1442.     size = strlen (standard_exec_prefix);
  1443.   if (strlen (standard_exec_prefix_1) > size)
  1444.     size = strlen (standard_exec_prefix_1);
  1445.   if (strlen (standard_startfile_prefix) > size)
  1446.     size = strlen (standard_startfile_prefix);
  1447.   if (strlen (standard_startfile_prefix_1) > size)
  1448.     size = strlen (standard_startfile_prefix_1);
  1449.   size += strlen (name) + strlen (target_name) + 1;
  1450.  
  1451.   temp = (char *) alloca (size);
  1452.  
  1453.   if (user_exec_prefix)
  1454.     {
  1455.       sprintf (temp, user_exec_prefix, target_name);
  1456.       strcat (temp, name);
  1457.       win = (access (temp, R_OK) == 0);
  1458.     }
  1459.  
  1460.   if (!win && env_exec_prefix)
  1461.     {
  1462.       sprintf (temp, env_exec_prefix, target_name);
  1463.       strcat (temp, name);
  1464.       win = (access (temp, R_OK) == 0);
  1465.     }
  1466.  
  1467.   if (!win)
  1468.     {
  1469.       sprintf (temp, standard_exec_prefix, target_name);
  1470.       strcat (temp, name);
  1471.       win = (access (temp, R_OK) == 0);
  1472.     }
  1473.  
  1474.   if (!win)
  1475.     {
  1476.       sprintf (temp, standard_exec_prefix_1, target_name);
  1477.       strcat (temp, name);
  1478.       win = (access (temp, R_OK) == 0);
  1479.     }
  1480.  
  1481.   if (!win)
  1482.     {
  1483.       sprintf (temp, standard_startfile_prefix, target_name);
  1484.       strcat (temp, name);
  1485.       win = (access (temp, R_OK) == 0);
  1486.     }
  1487.  
  1488.   if (!win)
  1489.     {
  1490.       sprintf (temp, standard_startfile_prefix_1, target_name);
  1491.       strcat (temp, name);
  1492.       win = (access (temp, R_OK) == 0);
  1493.     }
  1494.  
  1495.   if (!win)
  1496.     {
  1497.     strcpy (temp, "./");
  1498.     strcat (temp, name);
  1499.     win = (access (temp, R_OK) == 0);
  1500.     }
  1501.  
  1502.   if (win)
  1503.     return save_string (temp, strlen (temp));
  1504.   return name;
  1505. }
  1506.  
  1507. /* On fatal signals, delete all the temporary files.  */
  1508.  
  1509. void
  1510. fatal_error (signum)
  1511.      int signum;
  1512. {
  1513.   signal (signum, SIG_DFL);
  1514.   delete_failure_queue ();
  1515.   delete_temp_files ();
  1516.   /* Get the same signal again, this time not handled,
  1517.      so its normal effect occurs.  */
  1518.   kill (getpid (), signum);
  1519. }
  1520.  
  1521. int
  1522. main (argc, argv)
  1523.      int argc;
  1524.      char **argv;
  1525. {
  1526.   register int i;
  1527.   int value;
  1528.   int error_count = 0;
  1529.   int linker_was_run = 0;
  1530.   char *explicit_link_files;
  1531.   extern char *getenv();
  1532.   char *basename;
  1533.  
  1534.   programname = argv[0];
  1535.   if ((basename = strrchr(programname, '/')) != NULL) {
  1536.       ++basename;
  1537.   } else {
  1538.       basename = programname;
  1539.   }
  1540.   if (strcmp(basename, "g++") == 0) {
  1541.       cplusplus = 1;
  1542.   }
  1543.  
  1544.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1545.     signal (SIGINT, fatal_error);
  1546.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1547.     signal (SIGHUP, fatal_error);
  1548.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1549.     signal (SIGTERM, fatal_error);
  1550.  
  1551.   argbuf_length = 10;
  1552.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1553.  
  1554.   obstack_init (&obstack);
  1555.  
  1556.   choose_temp_base ();
  1557.  
  1558.   /* Make a table of what switches there are (switches, n_switches).
  1559.      Make a table of specified input files (infiles, n_infiles).  */
  1560.  
  1561.   process_command (argc, argv);
  1562.  
  1563.   /* Figure out the machine type we're running on, using the $MACHINE
  1564.     environment variable. */
  1565.  
  1566.   machine = getenv("MACHINE");
  1567.   if (machine == NULL)
  1568.     {
  1569.       machine = "";
  1570.     }
  1571.     machine_length = strlen(machine);
  1572.  
  1573.   /* If the target machine wasn't specified in a switch, then get it from
  1574.      the TM environment variable, if it exists, or next from the MACHINE
  1575.      environment variable.  If none of these exists, pick a default. */
  1576.  
  1577.    if (target == NULL)
  1578.      {
  1579.        char *env;
  1580.  
  1581.        env = getenv ("TM");
  1582.        if (env == NULL)
  1583.          {
  1584.        env = getenv ("MACHINE");
  1585.        if (env == NULL)
  1586.          {
  1587.            env = target_machines[0].name;
  1588.          }
  1589.      }
  1590.        set_target_machine (env);
  1591.      }
  1592.  
  1593.   if (target == NULL) {
  1594.       fprintf(stderr, "No target machine specified\n");
  1595.       exit(1);
  1596.   }
  1597.  
  1598.   if (vflag)
  1599.     {
  1600.       extern char *version_string;
  1601.       fprintf (stderr, "gcc version %s\n", version_string);
  1602.       fprintf (stderr, "target machine is %s\n", target_name);      
  1603.       if (n_infiles == 0)
  1604.     exit (0);
  1605.     }
  1606.  
  1607.   if (n_infiles == 0)
  1608.     fatal ("No input files specified.");
  1609.  
  1610.   /* Make a place to record the compiler output file names
  1611.      that correspond to the input files.  */
  1612.  
  1613.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1614.   bzero (outfiles, n_infiles * sizeof (char *));
  1615.  
  1616.   /* Record which files were specified explicitly as link input.  */
  1617.  
  1618.   explicit_link_files = (char *) xmalloc (n_infiles);
  1619.   bzero (explicit_link_files, n_infiles);
  1620.  
  1621.   for (i = 0; i < n_infiles; i++)
  1622.     {
  1623.       register struct compiler *cp;
  1624.       int this_file_error = 0;
  1625.  
  1626.       /* Tell do_spec what to substitute for %i.  */
  1627.  
  1628.       input_filename = infiles[i];
  1629.       input_filename_length = strlen (input_filename);
  1630.       input_file_number = i;
  1631.  
  1632.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1633.  
  1634.       outfiles[i] = input_filename;
  1635.  
  1636.       /* Figure out which compiler from the file's suffix.  */
  1637.  
  1638.       for (cp = (cplusplus ? target->cplusplus_specs : target->base_specs);
  1639.            cp->spec; cp++)
  1640.     {
  1641.       if (strlen (cp->suffix) < input_filename_length
  1642.           && !strcmp (cp->suffix,
  1643.               infiles[i] + input_filename_length
  1644.               - strlen (cp->suffix)))
  1645.         {
  1646.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1647.           /* First say how much of input_filename to substitute for %b  */
  1648.           register char *p;
  1649.  
  1650.           input_basename = input_filename;
  1651.           for (p = input_filename; *p; p++)
  1652.         if (*p == '/')
  1653.           input_basename = p + 1;
  1654.           basename_length = (input_filename_length - strlen (cp->suffix)
  1655.                  - (input_basename - input_filename));
  1656.           value = do_spec (cp->spec);
  1657.           if (value < 0)
  1658.         this_file_error = 1;
  1659.           break;
  1660.         }
  1661.     }
  1662.  
  1663.       /* If this file's name does not contain a recognized suffix,
  1664.      record it as explicit linker input.  */
  1665.  
  1666.       if (! cp->spec)
  1667.     explicit_link_files[i] = 1;
  1668.  
  1669.       /* Clear the delete-on-failure queue, deleting the files in it
  1670.      if this compilation failed.  */
  1671.  
  1672.       if (this_file_error)
  1673.     {
  1674.       delete_failure_queue ();
  1675.       error_count++;
  1676.     }
  1677.       /* If this compilation succeeded, don't delete those files later.  */
  1678.       clear_failure_queue ();
  1679.     }
  1680.  
  1681.   /* Run ld to link all the compiler output files.  */
  1682.  
  1683.   if (error_count == 0)
  1684.     {
  1685.       int tmp = execution_count;
  1686.       value = do_spec (target->link_base_spec);
  1687.       if (value < 0)
  1688.     error_count = 1;
  1689.       linker_was_run = (tmp != execution_count);
  1690.     }
  1691.  
  1692.   /* If options said don't run linker,
  1693.      complain about input files to be given to the linker.  */
  1694.  
  1695.   if (! linker_was_run && error_count == 0)
  1696.     for (i = 0; i < n_infiles; i++)
  1697.       if (explicit_link_files[i])
  1698.     error ("%s: linker input file unused since linking not done",
  1699.            outfiles[i]);
  1700.  
  1701.   /* Set the `valid' bits for switches that match anything in any spec.  */
  1702.  
  1703.   validate_all_switches ();
  1704.  
  1705.   /* Warn about any switches that no pass was interested in.  */
  1706.   
  1707.   for (i = 0; i < n_switches; i++)
  1708.     if (! switches[i].valid)
  1709.       error ("unrecognized option `-%s'", switches[i].part1);
  1710.  
  1711.   /* Delete some or all of the temporary files we made.  */
  1712.  
  1713.   if (error_count)
  1714.     delete_failure_queue ();
  1715.   delete_temp_files ();
  1716.  
  1717.   exit (error_count);
  1718. }
  1719.  
  1720. xmalloc (size)
  1721.      int size;
  1722. {
  1723.   register int value = malloc (size);
  1724.   if (value == 0)
  1725.     fatal ("Virtual memory full.");
  1726.   return value;
  1727. }
  1728.  
  1729. xrealloc (ptr, size)
  1730.      int ptr, size;
  1731. {
  1732.   register int value = realloc (ptr, size);
  1733.   if (value == 0)
  1734.     fatal ("Virtual memory full.");
  1735.   return value;
  1736. }
  1737.  
  1738. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1739.  
  1740. char *
  1741. concat (s1, s2, s3)
  1742.      char *s1, *s2, *s3;
  1743. {
  1744.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1745.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1746.  
  1747.   strcpy (result, s1);
  1748.   strcpy (result + len1, s2);
  1749.   strcpy (result + len1 + len2, s3);
  1750.   *(result + len1 + len2 + len3) = 0;
  1751.  
  1752.   return result;
  1753. }
  1754.  
  1755. char *
  1756. save_string (s, len)
  1757.      char *s;
  1758.      int len;
  1759. {
  1760.   register char *result = (char *) xmalloc (len + 1);
  1761.  
  1762.   bcopy (s, result, len);
  1763.   result[len] = 0;
  1764.   return result;
  1765. }
  1766.  
  1767. pfatal_with_name (name)
  1768.      char *name;
  1769. {
  1770.   extern int errno, sys_nerr;
  1771.   extern char *sys_errlist[];
  1772.   char *s;
  1773.  
  1774.   if (errno < sys_nerr)
  1775.     s = concat ("%s: ", sys_errlist[errno], "");
  1776.   else
  1777.     s = "cannot open %s";
  1778.   fatal (s, name);
  1779. }
  1780.  
  1781. perror_with_name (name)
  1782.      char *name;
  1783. {
  1784.   extern int errno, sys_nerr;
  1785.   extern char *sys_errlist[];
  1786.   char *s;
  1787.  
  1788.   if (errno < sys_nerr)
  1789.     s = concat ("%s: ", sys_errlist[errno], "");
  1790.   else
  1791.     s = "cannot open %s";
  1792.   error (s, name);
  1793. }
  1794.  
  1795. perror_exec (name)
  1796.      char *name;
  1797. {
  1798.   extern int errno, sys_nerr;
  1799.   extern char *sys_errlist[];
  1800.   char *s;
  1801.  
  1802.   if (errno < sys_nerr)
  1803.     s = concat ("installation problem, cannot exec %s: ",
  1804.         sys_errlist[errno], "");
  1805.   else
  1806.     s = "installation problem, cannot exec %s";
  1807.   error (s, name);
  1808. }
  1809.  
  1810. /* More 'friendly' abort that prints the line and file.
  1811.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  1812.  
  1813. void
  1814. fancy_abort ()
  1815. {
  1816.   fatal ("Internal gcc abort.");
  1817. }
  1818.  
  1819. #ifdef HAVE_VPRINTF
  1820.  
  1821. /* Output an error message and exit */
  1822.  
  1823. int 
  1824. fatal (va_alist)
  1825.      va_dcl
  1826. {
  1827.   va_list ap;
  1828.   char *format;
  1829.   
  1830.   va_start(ap);
  1831.   format = va_arg (ap, char *);
  1832.   vfprintf (stderr, format, ap);
  1833.   va_end (ap);
  1834.   fprintf (stderr, "\n");
  1835.   delete_temp_files (0);
  1836.   exit (1);
  1837. }  
  1838.  
  1839. error (va_alist) 
  1840.      va_dcl
  1841. {
  1842.   va_list ap;
  1843.   char *format;
  1844.  
  1845.   va_start(ap);
  1846.   format = va_arg (ap, char *);
  1847.   fprintf (stderr, "%s: ", programname);
  1848.   vfprintf (stderr, format, ap);
  1849.   va_end (ap);
  1850.  
  1851.   fprintf (stderr, "\n");
  1852. }
  1853.  
  1854. #else /* not HAVE_VPRINTF */
  1855.  
  1856. fatal (msg, arg1, arg2)
  1857.      char *msg, *arg1, *arg2;
  1858. {
  1859.   error (msg, arg1, arg2);
  1860.   delete_temp_files (0);
  1861.   exit (1);
  1862. }
  1863.  
  1864. error (msg, arg1, arg2)
  1865.      char *msg, *arg1, *arg2;
  1866. {
  1867.   fprintf (stderr, "%s: ", programname);
  1868.   fprintf (stderr, msg, arg1, arg2);
  1869.   fprintf (stderr, "\n");
  1870. }
  1871.  
  1872. #endif /* not HAVE_VPRINTF */
  1873.  
  1874.  
  1875. void
  1876. validate_all_switches ()
  1877. {
  1878.   struct compiler *comp;
  1879.   register char *p;
  1880.   register char c;
  1881.  
  1882.   for (comp = (cplusplus ? target->cplusplus_specs : target->base_specs);
  1883.        comp->spec; comp++)
  1884.     {
  1885.       p = comp->spec;
  1886.       while (c = *p++)
  1887.     if (c == '%' && *p == '{')
  1888.       /* We have a switch spec.  */
  1889.       validate_switches (p + 1);
  1890.     }
  1891.  
  1892.   p = target->link_base_spec;
  1893.   while (c = *p++)
  1894.     if (c == '%' && *p == '{')
  1895.       /* We have a switch spec.  */
  1896.       validate_switches (p + 1);
  1897.  
  1898.   /* Now notice switches mentioned in the machine-specific specs.  */
  1899.  
  1900. #ifdef ASM_SPEC
  1901.   p = ASM_SPEC;
  1902.   while (c = *p++)
  1903.     if (c == '%' && *p == '{')
  1904.       /* We have a switch spec.  */
  1905.       validate_switches (p + 1);
  1906. #endif
  1907.  
  1908. #ifdef CPP_SPEC
  1909.   p = CPP_SPEC;
  1910.   while (c = *p++)
  1911.     if (c == '%' && *p == '{')
  1912.       /* We have a switch spec.  */
  1913.       validate_switches (p + 1);
  1914. #endif
  1915.  
  1916. #ifdef SIGNED_CHAR_SPEC
  1917.   p = SIGNED_CHAR_SPEC;
  1918.   while (c = *p++)
  1919.     if (c == '%' && *p == '{')
  1920.       /* We have a switch spec.  */
  1921.       validate_switches (p + 1);
  1922. #endif
  1923.  
  1924. #ifdef CC1_SPEC
  1925.   p = CC1_SPEC;
  1926.   while (c = *p++)
  1927.     if (c == '%' && *p == '{')
  1928.       /* We have a switch spec.  */
  1929.       validate_switches (p + 1);
  1930. #endif
  1931.  
  1932. #ifdef LINK_SPEC
  1933.   p = LINK_SPEC;
  1934.   while (c = *p++)
  1935.     if (c == '%' && *p == '{')
  1936.       /* We have a switch spec.  */
  1937.       validate_switches (p + 1);
  1938. #endif
  1939.  
  1940. #ifdef LIB_SPEC
  1941.   p = LIB_SPEC;
  1942.   while (c = *p++)
  1943.     if (c == '%' && *p == '{')
  1944.       /* We have a switch spec.  */
  1945.       validate_switches (p + 1);
  1946. #endif
  1947.  
  1948. #ifdef STARTFILE_SPEC
  1949.   p = STARTFILE_SPEC;
  1950.   while (c = *p++)
  1951.     if (c == '%' && *p == '{')
  1952.       /* We have a switch spec.  */
  1953.       validate_switches (p + 1);
  1954. #endif
  1955. }
  1956.  
  1957. /* Look at the switch-name that comes after START
  1958.    and mark as valid all supplied switches that match it.  */
  1959.  
  1960. void
  1961. validate_switches (start)
  1962.      char *start;
  1963. {
  1964.   register char *p = start;
  1965.   char *filter;
  1966.   register int i;
  1967.  
  1968.   if (*p == '|')
  1969.     ++p;
  1970.  
  1971.   if (*p == '!')
  1972.     ++p;
  1973.  
  1974.   filter = p;
  1975.   while (*p != ':' && *p != '}') p++;
  1976.  
  1977.   if (p[-1] == '*')
  1978.     {
  1979.       /* Mark all matching switches as valid.  */
  1980.       --p;
  1981.       for (i = 0; i < n_switches; i++)
  1982.     if (!strncmp (switches[i].part1, filter, p - filter))
  1983.       switches[i].valid = 1;
  1984.     }
  1985.   else
  1986.     {
  1987.       /* Mark an exact matching switch as valid.  */
  1988.       for (i = 0; i < n_switches; i++)
  1989.     {
  1990.       if (!strncmp (switches[i].part1, filter, p - filter)
  1991.           && switches[i].part1[p - filter] == 0)
  1992.         switches[i].valid = 1;
  1993.     }
  1994.     }
  1995. }
  1996. @
  1997.  
  1998.  
  1999. 1.7
  2000. log
  2001. @*** empty log message ***
  2002. @
  2003. text
  2004. @d121 1
  2005. d162 1
  2006. a163 1
  2007.  
  2008. d1142 4
  2009. d1474 1
  2010. d1477 8
  2011. d1580 2
  2012. a1581 1
  2013.       for (cp = target->base_specs; cp->spec; cp++)
  2014. d1824 2
  2015. a1825 1
  2016.   for (comp = target->base_specs; comp->spec; comp++)
  2017. @
  2018.  
  2019.  
  2020. 1.6
  2021. log
  2022. @Change default path from /sprite/cmds to /sprite/lib/gcc.
  2023. @
  2024. text
  2025. @d754 1
  2026. a754 1
  2027.     break;
  2028. d1520 5
  2029. @
  2030.  
  2031.  
  2032. 1.5
  2033. log
  2034. @*** empty log message ***
  2035. @
  2036. text
  2037. @d223 2
  2038. a224 2
  2039. char *standard_exec_prefix = "/sprite/cmds.%s/";
  2040. char *standard_exec_prefix_1 = "/sprite/lib/gcc/%s.md/";
  2041. @
  2042.  
  2043.  
  2044. 1.4
  2045. log
  2046. @*** empty log message ***
  2047. @
  2048. text
  2049. @d6 5
  2050. d12 3
  2051. a14 13
  2052. but WITHOUT ANY WARRANTY.  No author or distributor
  2053. accepts responsibility to anyone for the consequences of using it
  2054. or for whether it serves any particular purpose or works at all,
  2055. unless he says so in writing.  Refer to the GNU CC General Public
  2056. License for full details.
  2057.  
  2058. Everyone is granted permission to copy, modify and redistribute
  2059. GNU CC, but only under the conditions described in the
  2060. GNU CC General Public License.   A copy of this license is
  2061. supposed to have been given to you along with GNU CC so you
  2062. can know your rights and responsibilities.  It should be in a
  2063. file named COPYING.  Among other things, the copyright notice
  2064. and this notice must be preserved on all copies.  */
  2065. d16 6
  2066. d23 2
  2067. d78 8
  2068. a85 15
  2069.  %c     process target->signed_char_spec as a spec.
  2070.  %C    process target->cpp_spec as a spec.  This gives target-specific
  2071.     switches for the C compiler, including the name of the compiler.
  2072.  %a     process target->asm_spec as a spec.
  2073.         This allows specs.h to specify part of the spec for running the
  2074.     assembler, including the name of the assembler.
  2075.  %l     process target->link_spec as a spec.  Spec must start with name
  2076.     of linker.
  2077.  %L     process target->lib_spec as a spec.
  2078.  %m    substitute the official name of the target machine (may be
  2079.     different than the name specified in the -m switch, if there are
  2080.     several unofficial names for the same machine).
  2081.  %S     process target->startfile_spec as a spec.  A capital S is actually
  2082.     used here.
  2083.  %1    process target->cc1_spec as a spec.
  2084. d103 3
  2085. d115 1
  2086. a115 1
  2087. be treated as output files, and passed to the linker in their proper
  2088. a119 8
  2089. /* This defines which switches take arguments.  */
  2090.  
  2091. #define SWITCH_TAKES_ARG(CHAR)      \
  2092.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  2093.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  2094.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  2095.    || (CHAR) == 'L' || (CHAR) == 'B')
  2096.  
  2097. a123 1
  2098. #ifdef sun4
  2099. d125 2
  2100. a126 1
  2101. #endif
  2102. d128 1
  2103. a128 2
  2104.  
  2105. extern int errno;
  2106. d158 3
  2107. d162 1
  2108. d168 1
  2109. a168 3
  2110.  
  2111. /* Record the names of temporary files we tell compilers to write,
  2112.    and delete them at the end of the run.  */
  2113. d170 8
  2114. a177 6
  2115. /* This is the common prefix we use to make temp file names.
  2116.    It is chosen once for each run of this program.
  2117.    It is substituted into a spec by %g.
  2118.    Thus, all temp file names contain this prefix.
  2119.    In practice, all temp file names start with this prefix.
  2120.    The prefix starts with `/tmp'.  */
  2121. d179 1
  2122. a179 1
  2123. char *temp_filename;
  2124. d181 3
  2125. a183 81
  2126. /* Length of the prefix.  */
  2127.  
  2128. int temp_filename_length;
  2129.  
  2130. /* Define the list of temporary files to delete.  */
  2131.  
  2132. struct temp_file
  2133. {
  2134.   char *name;
  2135.   struct temp_file *next;
  2136.   int success_only;        /* Nonzero means delete this file
  2137.                    only if compilation succeeds fully.  */
  2138.   int fail_only;        /* Nonzero means delete this file
  2139.                    only if compilation fails.  */
  2140. };
  2141.  
  2142. struct temp_file *temp_file_queue;
  2143.  
  2144. /* Record FILENAME as a file to be deleted automatically.
  2145.    SUCCESS_ONLY nonzero means delete it only if all compilation succeeds;
  2146.    otherwise delete it in any case.
  2147.    FAIL_ONLY nonzero means delete it only if a compilation step fails;
  2148.    otherwise delete it in any case.  */
  2149.  
  2150. void
  2151. record_temp_file (filename, success_only, fail_only)
  2152.      char *filename;
  2153.      int success_only;
  2154.      int fail_only;
  2155. {
  2156.   register struct temp_file *temp;
  2157.   register char *name;
  2158.   temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  2159.   name = (char *) xmalloc (strlen (filename) + 1);
  2160.   strcpy (name, filename);
  2161.   temp->next = temp_file_queue;
  2162.   temp->name = name;
  2163.   temp->success_only = success_only;
  2164.   temp->fail_only = fail_only;
  2165.   temp_file_queue = temp;
  2166. }
  2167.  
  2168. /* Delete all the temporary files whose names we previously recorded.
  2169.    SUCCESS nonzero means "delete on success only" files should be deleted.
  2170.    When we use -pipe option, delete all tempoarry
  2171.    without checking of success flag.  */
  2172.  
  2173. void
  2174. delete_temp_files (success)
  2175.      int success;
  2176. {
  2177.   register struct temp_file *temp;
  2178.   for (temp = temp_file_queue; temp; temp = temp->next)
  2179.     if (success ? !temp->fail_only : !temp->success_only)
  2180.       {
  2181. #ifdef DEBUG
  2182.     int i;
  2183.     printf ("Delete %s? (y or n) ", temp->name);
  2184.     fflush (stdout);
  2185.     i = getchar ();
  2186.     if (i != '\n')
  2187.       while (getchar () != '\n') ;
  2188.     if (i == 'y' || i == 'Y')
  2189. #endif /* DEBUG */
  2190.       unlink (temp->name);
  2191.       }
  2192.   temp_file_queue = 0;
  2193. }
  2194.  
  2195. /* Compute a string to use as the base of all temporary file names.
  2196.    It is substituted for %g.  */
  2197.  
  2198. void
  2199. choose_temp_base ()
  2200. {
  2201.   register char *foo = "/tmp/ccXXXXXX";
  2202.   temp_filename = (char *) xmalloc (strlen (foo) + 1);
  2203.   strcpy (temp_filename, foo);
  2204.   mktemp (temp_filename);
  2205.   temp_filename_length = strlen (temp_filename);
  2206. }
  2207. d199 4
  2208. d207 4
  2209. a218 1
  2210. char *env_exec_prefix = 0;
  2211. d220 2
  2212. a221 2
  2213.    "%s", then it will be replaced with the type of machine on which
  2214.    we're running, to get the correct binaries for this machine. */
  2215. d230 1
  2216. a230 1
  2217.    binaries for programs like cpp). */
  2218. d235 12
  2219. d248 1
  2220. a248 1
  2221. #define STANDARD_STARTFILE_PREFIX "/lib/"
  2222. d261 4
  2223. a264 6
  2224.    If TEMPNAMEP is nonzero, this arg is a file that should be deleted
  2225.    at the end of compilation.
  2226.    If TEMPNAMEP is 2, delete the file
  2227.    only if compilation is fully successful.
  2228.    If TEMPNAMEP is 3, delete the file
  2229.    only if a compilation step fails.  */
  2230. d267 1
  2231. a267 1
  2232. store_arg (arg, tempnamep)
  2233. d269 1
  2234. a269 1
  2235.      int tempnamep;
  2236. d279 173
  2237. a451 2
  2238.   if (tempnamep)
  2239.     record_temp_file (arg, tempnamep == 2, tempnamep == 3);
  2240. d473 2
  2241. d481 1
  2242. a481 1
  2243.       sprintf (temp, user_exec_prefix, machine);
  2244. d503 1
  2245. a503 1
  2246.       strcat (temp, argbuf[0]);
  2247. d590 1
  2248. a590 1
  2249.       fprintf(stderr, "Can't exec %s: %s\n", program, strerror(errno));
  2250. d706 2
  2251. d741 24
  2252. a764 1
  2253.  
  2254. d769 3
  2255. a771 1
  2256.    is the switch itself and the `part2' field is the following argument.  */
  2257. d777 1
  2258. a793 44
  2259. char *
  2260. make_switch (p1, s1, p2, s2)
  2261.      char *p1;
  2262.      int s1;
  2263.      char *p2;
  2264.      int s2;
  2265. {
  2266.   register char *new;
  2267.   if (p2 && s2 == 0)
  2268.     s2 = strlen (p2);
  2269.   new = (char *) xmalloc (s1 + s2 + 2);
  2270.   bcopy (p1, new, s1);
  2271.   if (p2)
  2272.     {
  2273.       new[s1++] = ' ';
  2274.       bcopy (p2, new + s1, s2);
  2275.     }
  2276.   new[s1 + s2] = 0;
  2277.   return new;
  2278. }
  2279.  
  2280. void
  2281. set_target_machine(name)
  2282.     char *name;
  2283. {
  2284.   register target_machine *t;
  2285.   int i;
  2286.  
  2287.   for (i = 0; ; i++)
  2288.     {
  2289.       t = &target_machines[i];
  2290.       if (t->name == 0)
  2291.         {
  2292.       break;
  2293.     }
  2294.       if (strcmp(t->name, name) == 0)
  2295.         {
  2296.       target_name = t->info->name;
  2297.       target = t->info;
  2298.       return;
  2299.     }
  2300.     }
  2301. }
  2302.  
  2303. d811 1
  2304. a811 1
  2305.      Here we also parse the switches that cc itself uses (e.g. -v, -m).  */
  2306. d820 1
  2307. a820 2
  2308.       p++;
  2309.       if (SWITCH_TAKES_ARG (c) && *p == 0)
  2310. d822 3
  2311. a824 5
  2312.           i++;
  2313.           p = argv[i];
  2314.           if (p == NULL)
  2315.             p = "";
  2316.         }
  2317. a825 2
  2318.       switch (c)
  2319.         {
  2320. d827 1
  2321. a827 1
  2322.           user_exec_prefix = p;
  2323. d840 1
  2324. a840 1
  2325.           set_target_machine(p);
  2326. d846 5
  2327. d876 2
  2328. d879 2
  2329. a880 1
  2330.       if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  2331. d884 1
  2332. d886 1
  2333. a886 1
  2334.         n_switches++;
  2335. d996 1
  2336. a996 4
  2337.         if (this_is_output_file)
  2338.           store_arg (string, 3);
  2339.         else
  2340.           store_arg (string, delete_this_arg);
  2341. d1013 4
  2342. a1016 1
  2343.           break;
  2344. d1043 1
  2345. a1044 4
  2346.           store_arg (string, 3);
  2347.         else
  2348.           store_arg (string, delete_this_arg);
  2349.         if (this_is_output_file)
  2350. d1062 1
  2351. a1062 4
  2352.         if (this_is_output_file)
  2353.           store_arg (string, 3);
  2354.         else
  2355.           store_arg (string, delete_this_arg);
  2356. d1117 1
  2357. a1117 1
  2358.         store_arg (outfiles[f], 0);
  2359. d1166 3
  2360. a1168 3
  2361.           obstack_grow (&obstack, target_name, strlen (target_name));
  2362.           arg_going = 1;
  2363.           break;
  2364. d1176 1
  2365. a1176 1
  2366.           char *x = (char *) alloca (strlen (target->cpp_predefines) * 2 + 1);
  2367. d1277 1
  2368. a1277 3
  2369.       {
  2370.         give_switch (i);
  2371.       }
  2372. d1293 1
  2373. a1294 1
  2374.           break;
  2375. d1306 1
  2376. d1332 1
  2377. a1332 1
  2378.       do_spec_1 ("-");
  2379. d1356 1
  2380. d1378 2
  2381. d1386 1
  2382. a1386 1
  2383.   size += strlen (name) + strlen(target_name) + 1;
  2384. d1420 1
  2385. a1420 1
  2386.       strcpy (temp, standard_startfile_prefix);
  2387. d1427 1
  2388. a1427 1
  2389.       strcpy (temp, standard_startfile_prefix_1);
  2390. d1434 3
  2391. a1436 3
  2392.       strcpy (temp, "./");
  2393.       strcat (temp, name);
  2394.       win = (access (temp, R_OK) == 0);
  2395. a1443 4
  2396. /* Name with which this program was invoked.  */
  2397.  
  2398. char *programname;
  2399.  
  2400. d1451 2
  2401. a1452 1
  2402.   delete_temp_files (0);
  2403. a1464 1
  2404.   int nolink = 0;
  2405. d1466 2
  2406. d1492 1
  2407. a1492 1
  2408.      environment variable. */
  2409. d1499 1
  2410. a1499 1
  2411.   machine_length = strlen(machine);
  2412. d1505 3
  2413. a1507 3
  2414.   if (target == NULL)
  2415.     {
  2416.       char *env;
  2417. d1509 11
  2418. a1519 11
  2419.       env = getenv ("TM");
  2420.       if (env == NULL)
  2421.         {
  2422.       env = getenv ("MACHINE");
  2423.       if (env == NULL)
  2424.         {
  2425.           env = target_machines[0].name;
  2426.         }
  2427.     }
  2428.       set_target_machine (env);
  2429.     }
  2430. d1525 1
  2431. a1525 1
  2432.       fprintf (stderr, "target machine is %s\n", target_name);
  2433. d1539 5
  2434. d1547 1
  2435. d1580 1
  2436. a1580 1
  2437.         error_count = 1;
  2438. d1586 1
  2439. a1586 2
  2440.      don't do anything to it, but do feed it to the link spec
  2441.      since its name is in outfiles.  */
  2442. d1588 10
  2443. a1597 6
  2444.       if (! cp->spec && nolink)
  2445.     {
  2446.       /* But if this happens, and we aren't going to run the linker,
  2447.          warn the user.  */
  2448.       error ("%s: linker input file unused since linking not done",
  2449.          input_filename);
  2450. d1599 2
  2451. d1605 1
  2452. a1605 1
  2453.   if (! nolink && error_count == 0)
  2454. d1607 1
  2455. d1611 1
  2456. d1614 19
  2457. d1635 3
  2458. a1637 1
  2459.   delete_temp_files (error_count == 0);
  2460. a1659 16
  2461. fatal (msg, arg1, arg2)
  2462.      char *msg, *arg1, *arg2;
  2463. {
  2464.   error (msg, arg1, arg2);
  2465.   delete_temp_files (0);
  2466.   exit (1);
  2467. }
  2468.  
  2469. error (msg, arg1, arg2)
  2470.      char *msg, *arg1, *arg2;
  2471. {
  2472.   fprintf (stderr, "%s: ", programname);
  2473.   fprintf (stderr, msg, arg1, arg2);
  2474.   fprintf (stderr, "\n");
  2475. }
  2476.  
  2477. d1697 1
  2478. a1697 1
  2479.     s = concat ("", sys_errlist[errno], " for %s");
  2480. d1711 1
  2481. a1711 1
  2482.     s = concat ("", sys_errlist[errno], " for %s");
  2483. d1715 201
  2484. @
  2485.  
  2486.  
  2487. 1.3
  2488. log
  2489. @added sun4 stuff
  2490. @
  2491. text
  2492. @d133 3
  2493. @
  2494.  
  2495.  
  2496. 1.2
  2497. log
  2498. @*** empty log message ***
  2499. @
  2500. text
  2501. @d135 2
  2502. d481 1
  2503. @
  2504.  
  2505.  
  2506. 1.1
  2507. log
  2508. @Initial revision
  2509. @
  2510. text
  2511. @d75 15
  2512. a89 8
  2513.  %a     process ASM_SPEC as a spec.
  2514.         This allows config.h to specify part of the spec for running as.
  2515.  %l     process LINK_SPEC as a spec.
  2516.  %L     process LIB_SPEC as a spec.
  2517.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  2518.  %c    process SIGNED_CHAR_SPEC as a spec.
  2519.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  2520.  %1    process CC1_SPEC as a spec.
  2521. d127 1
  2522. a127 1
  2523.    || (CHAR) == 'L')
  2524. a132 1
  2525. #include "config.h"
  2526. d164 3
  2527. a166 5
  2528. /* config.h can define ASM_SPEC to provide extra args to the assembler
  2529.    or extra switch-translations.  */
  2530. #ifndef ASM_SPEC
  2531. #define ASM_SPEC ""
  2532. #endif
  2533. d168 1
  2534. a168 107
  2535. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  2536.    or extra switch-translations.  */
  2537. #ifndef CPP_SPEC
  2538. #define CPP_SPEC ""
  2539. #endif
  2540.  
  2541. /* config.h can define CC1_SPEC to provide extra args to cc1
  2542.    or extra switch-translations.  */
  2543. #ifndef CC1_SPEC
  2544. #define CC1_SPEC ""
  2545. #endif
  2546.  
  2547. /* config.h can define LINK_SPEC to provide extra args to the linker
  2548.    or extra switch-translations.  */
  2549. #ifndef LINK_SPEC
  2550. #define LINK_SPEC ""
  2551. #endif
  2552.  
  2553. /* config.h can define LIB_SPEC to override the default libraries.  */
  2554. #ifndef LIB_SPEC
  2555. #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  2556. #endif
  2557.  
  2558. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  2559. #ifndef STARTFILE_SPEC
  2560. #define STARTFILE_SPEC  \
  2561.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  2562. #endif
  2563.  
  2564. /* This spec is used for telling cpp whether char is signed or not.  */
  2565. #define SIGNED_CHAR_SPEC  \
  2566.   (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}"    \
  2567.    : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
  2568.  
  2569. /* This structure says how to run one compiler, and when to do so.  */
  2570.  
  2571. struct compiler
  2572. {
  2573.   char *suffix;            /* Use this compiler for input files
  2574.                    whose names end in this suffix.  */
  2575.   char *spec;            /* To use this compiler, pass this spec
  2576.                    to do_spec.  */
  2577. };
  2578.  
  2579. /* Here are the specs for compiling files with various known suffixes.
  2580.    A file that does not end in any of these suffixes will be passed
  2581.    unchanged to the loader and nothing else will be done to it.  */
  2582.  
  2583. struct compiler compilers[] =
  2584. {
  2585.   {".c",
  2586.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{T} \
  2587.         -undef -D__GNUC__ %{ansi:-T -$ -D__STRICT_ANSI__} %{!ansi:%p} %P\
  2588.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  2589.     %{Wcomment} %{Wtrigraphs} %{Wall} %C\
  2590.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  2591.     %{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \
  2592.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*}\
  2593.            %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  2594.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  2595.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  2596.            %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  2597.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  2598.                       %{!pipe:%g.s}\
  2599.               %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\n }}}"},
  2600.   {".cc",
  2601.    "cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{T} \
  2602.         -undef -D__GNUC__ %p %P\
  2603.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  2604.     %{Wcomment} %{Wtrigraphs} %{Wall} %C\
  2605.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  2606.     %{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\
  2607.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*}\
  2608.            %{g} %{O} %{W*} %{w} %{pedantic} %{traditional}\
  2609.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  2610.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  2611.            %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  2612.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  2613.                       %{!pipe:%g.s}\
  2614.               %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\n }}}"},
  2615.   {".i",
  2616.    "cc1 %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{f*}\
  2617.     %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  2618.     %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  2619.     %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  2620.     %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  2621.             %{!pipe:%g.s} %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\n }"},
  2622.   {".s",
  2623.    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  2624.             %i %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\n }"},
  2625.   {".S",
  2626.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{T} \
  2627.         -undef -D__GNUC__ -$ %p %P\
  2628.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  2629.     %{Wcomment} %{Wtrigraphs} %{Wall} %C\
  2630.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  2631.     %{!M*:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{!pipe:%g.s} \
  2632.                     %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\n }}}"},
  2633.   /* Mark end of table */
  2634.   {0, 0}
  2635. };
  2636.  
  2637. /* Here is the spec for running the linker, after compiling all files.  */
  2638. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  2639.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  2640.  %{y*} %{!nostdlib:%S} \
  2641.  %{L*} %o %{!nostdlib:gnulib%s %{g:-lg} %L}\n }}}}";
  2642. d291 3
  2643. d295 2
  2644. a296 1
  2645. /* Default prefixes to attach to command names.  */
  2646. d298 2
  2647. a299 3
  2648. #ifndef STANDARD_EXEC_PREFIX
  2649. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  2650. #endif /* !defined STANDARD_EXEC_PREFIX */
  2651. d301 2
  2652. a302 2
  2653. char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  2654. char *standard_exec_prefix_1 = "/usr/lib/gcc-";
  2655. d304 3
  2656. a310 3
  2657. char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  2658. char *standard_startfile_prefix_1 = "/usr/lib/";
  2659.  
  2660. d363 1
  2661. a363 1
  2662.   size += strlen (prog) + 1;
  2663. d370 1
  2664. a370 1
  2665.       strcpy (temp, user_exec_prefix);
  2666. d377 1
  2667. a377 1
  2668.       strcpy (temp, env_exec_prefix);
  2669. d384 1
  2670. a384 1
  2671.       strcpy (temp, standard_exec_prefix);
  2672. d391 1
  2673. a391 1
  2674.       strcpy (temp, standard_exec_prefix_1);
  2675. d675 23
  2676. d715 1
  2677. a715 1
  2678.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  2679. d724 9
  2680. d736 1
  2681. a736 1
  2682.           user_exec_prefix = p + 1;
  2683. d744 9
  2684. a754 3
  2685.  
  2686.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  2687.         i++;
  2688. a779 2
  2689.       if (c == 'B')
  2690.         continue;
  2691. d785 2
  2692. a786 1
  2693.       n_switches++;
  2694. d1048 1
  2695. a1048 1
  2696.         do_spec_1 (CC1_SPEC, 0);
  2697. d1052 1
  2698. a1052 1
  2699.         do_spec_1 (ASM_SPEC, 0);
  2700. d1056 1
  2701. a1056 1
  2702.         do_spec_1 (SIGNED_CHAR_SPEC, 0);
  2703. d1060 1
  2704. a1060 1
  2705.         do_spec_1 (CPP_SPEC, 0);
  2706. d1064 1
  2707. a1064 1
  2708.         do_spec_1 (LINK_SPEC, 0);
  2709. d1068 1
  2710. a1068 1
  2711.         do_spec_1 (LIB_SPEC, 0);
  2712. d1071 5
  2713. d1077 1
  2714. a1077 1
  2715.         do_spec_1 (CPP_PREDEFINES, 0);
  2716. d1082 1
  2717. a1082 1
  2718.           char *x = (char *) alloca (strlen (CPP_PREDEFINES) * 2 + 1);
  2719. d1084 1
  2720. a1084 1
  2721.           char *y = CPP_PREDEFINES;
  2722. d1086 1
  2723. a1086 1
  2724.           /* Copy all of CPP_PREDEFINES into BUF,
  2725. d1117 1
  2726. a1117 1
  2727.         do_spec_1 (STARTFILE_SPEC, 0);
  2728. d1121 1
  2729. a1121 1
  2730.         abort ();
  2731. d1290 1
  2732. a1290 1
  2733.   size += strlen (name) + 1;
  2734. d1296 1
  2735. a1296 1
  2736.       strcpy (temp, user_exec_prefix);
  2737. d1303 1
  2738. a1303 1
  2739.       strcpy (temp, env_exec_prefix);
  2740. d1310 1
  2741. a1310 1
  2742.       strcpy (temp, standard_exec_prefix);
  2743. d1317 1
  2744. a1317 1
  2745.       strcpy (temp, standard_exec_prefix_1);
  2746. d1374 1
  2747. d1397 30
  2748. d1431 1
  2749. d1461 1
  2750. a1461 1
  2751.       for (cp = compilers; cp->spec; cp++)
  2752. d1502 1
  2753. a1502 1
  2754.       value = do_spec (link_spec);
  2755. @
  2756.